package com.br.cashflow.bo;

import com.br.cashflowdao.hibernate.dao.GenericDAO;
import com.br.cashflowdao.hibernate.dao.entities.CreditTransaction;
import com.br.cashflowdao.hibernate.dao.factory.DAOFactory;
import com.br.cashflowdao.hibernate.dao.factory.MySqlDAOFactory;

/**
 * Credit Transaction Business Object.
 * This class
 * @author Marcel Sena <marcel.sena at marcel.sena@gmail.com>
 */
public class CreditTransactionBO {

    /**
     * Adds the given credit transaction
     *
     * @param expects a <code>CreditTransaction</code> object
     * @return the current state of the <code>CreditTransaction</code> object with its id filled
     * @exception throws <code>IllegalArgumentException</code>
     */
    public CreditTransaction createCredit(CreditTransaction credit) throws IllegalArgumentException {

        if (credit == null)
            throw new IllegalArgumentException("CreditTransaction object can't be null");

        DAOFactory factory = DAOFactory.getInstance(MySqlDAOFactory.class);
        GenericDAO dao = factory.getGenericDAO();
        CreditTransaction result = (CreditTransaction) dao.createOrUpdate(credit);
        return result;
    }

    /**
     * Removes the given credit transaction
     * 
     * @param expects a <code>CreditTransaction</code> object
     * @exception throws <code>IllegalArgumentException</code>
     */
    public void removeCredit(CreditTransaction credit) {
        DAOFactory factory = DAOFactory.getInstance(MySqlDAOFactory.class);
        GenericDAO dao = factory.getGenericDAO();
        dao.remove(credit);
    }

   /**
    * Verifies if the given <code>CreditTransaction</code> object is 
    * in the correct state to be removed.
    * <p>
    *
    * @param expects a <code>CreditTransaction</code> object to be checked
    * @return <code>false</code> if the given parameter is either <code>null</code>
    * or if its id attribute is <code>null</code>. 
    * Else it returns <code>true</code> if the given <code>CreditTransaction</code> object
    * is not <code>null</code>
    * and it has its id attribute).
    * @exception throws <code>IllegalArgumentException</code>
    */
   private boolean isValidToRemove(CreditTransaction credit){
        if (credit == null)
            return false;
        else if (credit.getId() == null)
            return false;
        return true;
    }

  /**
   * Verifies if the given <code>CreditTransaction</code> object is
   * in the correct state to be created.
   * <p>
   *
   * @param expects a <code>CreditTransaction</code> object to be checked
   * @return <code>false</code> if any of the following <code>CreditTransaction</code> 
   * required attributes are missing: Id, Account, Category, Amount,
   * , CreateDate, CreditDate, UpdateDate, Payee.
   * Else it returns <code>true</code> if the given <code>CreditTransaction</code> object
   * is not <code>null</code> and it has all its required attributes (already mentioned above).
   * @exception throws <code>IllegalArgumentException</code>
   */
  private boolean isValidToCreate(CreditTransaction credit){
        if (credit == null)
            return false;
        else if (credit.getId() == null)
            return false;
      return true;
   }
}
